package com.effvi.taoke.services.spider;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.effvi.taoke.services.spider.taobao.bean.SuggestWords;
import com.effvi.taoke.utils.HttpCommon;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class TaobaoKeywordsSpider {
	private JsonParser jsonParser = new JsonParser();
	private String searchUrl ="http://s.taobao.com/search?spm=a230r.1.4.4.US0JSK&rsclick=4&q=";
//	private String searchUrl ="http://suggest.taobao.com/sug?code=utf-8&q=";
	public static void main(String[] args) {
//		TaobaoKeywordsSpider taobao = new TaobaoKeywordsSpider();
//		long start = System.currentTimeMillis();
//		for(int i=1;i<100000;i++){
//			String mainWord ="男装";
//			String jsonStr = taobao.getSuggest(mainWord);
//			Document doc = Jsoup.parse(jsonStr);
//			Elements eles = doc.select("a[trace=relatedSearch]");
////			System.out.println(eles.size());
//			for(Element e : eles){
////				System.out.println(e.text());
//			}
//			if(eles.size() == 0){
//				System.out.println(System.currentTimeMillis()-start);
//				break;
//			}
//			System.out.println(i+"===="+eles.size());
//		}
		
//		System.out.println(jsonStr);
//		SuggestWords suggest = taobao.getSuggestBean(mainWord);
		
//		JsonArray arrays = suggest.getSuggestWords();
//		for(int i=0; i < arrays.size();i++){
//			JsonArray array  = arrays.get(i).getAsJsonArray();
//			System.out.println(array.toString());
////			System.out.println(arrays.get(i));
//		}
//		
		
		
		String cookies ="miid=106080075473977556; cna=GVmKCKvL0HICAYbMdHFGlpEg; sc4=1; x=e%3D1%26p%3D*%26s%3D0%26c%3D1%26f%3D0%26g%3D0%26t%3D0; _tb_token_=YQSSxmEo7OA0; mpp=t%3D0%26m%3D%26h%3D0%26l%3D0; tk_pass=TfnVDEQRsNDnFfTaG8OhaldKopCc10zK; linezing_session=ie20T2nNT7uoynhfgriXcaEt_1376976456_19; sc3=2; l=ducter2::1376980360259::11; ck1=; v=0; lzstat_uv=11939324762366092254|2881505@2208862@2852799@2606665@2606628@2144678@1836558@1836634@1738929@2511607@2511613@1977649@2618227@2862317@2769016@2199358@2836758@2341454@2738597@1023331@1791451@3036400@1759261@2938535@2879138@3010391@2848884@2581747@3284827@2581759; lzstat_ss=3050290976_1_1377007053_2581747|1876486218_5_1377007101_3284827|1007028350_3_1377007099_2581759; tg=5; _cc_=VFC%2FuZ9ajQ%3D%3D; t=486f539515cc991b335b6094b8c3108c; unb=1792344514; _nk_=taobao_zhishu; _l_g_=Ug%3D%3D; cookie2=e10b1cf983c596af029d46cf1d2a002c; tracknick=taobao_zhishu; sg=u44; lgc=taobao_zhishu; lastgetwwmsg=MTM3Njk3ODYyNw%3D%3D; cookie1=W8CJ%2F8nvrIJr52hp6ZtxA04bWtwXvafc5IMOJdCBX2U%3D; uc3=nk2=F5fTsrszEk8tqdDcQQ%3D%3D&id2=UoYXLufTxjiEzg%3D%3D&lg2=WqG3DMC9VAQiUQ%3D%3D; cookie17=UoYXLufTxjiEzg%3D%3D; sc1=s%3A4lzbGkMUTBaQleUUfQMzKMIB.iipy366ykCIFPbq%2B0LXwAJL6M3qXGHzKtDnaO4V84cQ; mt=cp=0&ci=0_1; uc1=lltime=1376977650&cookie14=UoLbuUmlWoRN3Q%3D%3D&existShop=false&cookie16=UtASsssmPlP%2Ff1IHDsDaPRu%2BPw%3D%3D&cookie21=UIHiLt3xThH8t7YRULGW&tag=7&cookie15=V32FPkk%2Fw0dUvg%3D%3D; sc2=tiiqrleaBA; sc5=068u5ObXObBitH2MRYO9Oz0bASM1EzUrOTDGcM%3D%7CuKBnf0c%2FV89HXzc%2Fp5%2B37zU%3D%7CuZFW7MvyeVWudS4JzunBqY5JbhSPZC%2FUqJSzabM%3D%7CvoZB%2B%2FM0PPvjq4O7fGTsZKO787uzdOzk7CszezM7%2FHRMJP4k%7Cvzfw1%2FAq%7CvCTjxOM5%7CvaWNSm11TTVdxU1VPTWtlY2lDQXiysL68rqyyqI6QspSagJ64vrdBw%3D%3D%7Csqoy9U9XkAgweL%2Bnv3hfmLB3X8gSqCRf2K%2BIUog%3D%7Cs%2Fvz%2Bzw089scdLPb0xRsBMPb42uslMyEXg%3D%3D; sc7=1";
		String zhishuUrl = "http://shu.taobao.com/top/16/search?spm=0.0.0.0.eopX1W";
		try {
			String indexDoc = HttpCommon.get(zhishuUrl, cookies);
//			System.out.println(indexDoc);
			Document doc = Jsoup.parse(indexDoc);
			Elements eles = doc.select("div#cate-section ul li");
			List<Map<String,String>> data = new ArrayList<Map<String,String>>();
			for(Element el : eles){
				String rank = el.select("small").text();
				String ctitle = el.select("a").text();
				String cid = el.select("a").get(0).attr("href").replaceAll("\\D", "");
				if(!"".equals(rank) && !"".equals(cid) && !"".equals(ctitle)){
					Map<String,String> map = new HashMap<String,String>();
					map.put("rank", rank);
					map.put("cid", cid);
					map.put("ctitle", ctitle);
					System.out.println(rank+","+cid+","+ctitle+"  =====>已添加至集合");
					data.add(map);
				}
				System.out.println(rank+","+cid+","+ctitle+"  =====>已添加至集合");
			}
			
			System.out.println(eles.toString());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public String getSuggest(String keyWord){
		String docStr = null;
		try {
			String key = URLEncoder.encode(keyWord);
			docStr = HttpCommon.get(searchUrl+key, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return docStr;
	}
	
	public SuggestWords getSuggestBean(String keyWord){
		String docStr = null;
		try {
			String key = URLEncoder.encode(keyWord);
			docStr = HttpCommon.get(searchUrl+key, null);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return convertResultToBean(keyWord,docStr);
	}
	
	private SuggestWords convertResultToBean(String mainWord,String jsonResult){
		JsonElement element = jsonParser.parse(jsonResult);
		JsonObject json = element.getAsJsonObject();
		SuggestWords suggest = new SuggestWords();
		suggest.setMainWord(mainWord);
		suggest.setSuggestWords(json.get("result").getAsJsonArray());
		return suggest;
	}
}
